package com.sch.hsy.web.controller.system;

import com.alibaba.druid.support.json.JSONUtils;
import com.sch.hsy.common.constant.ShiroConstants;
import com.sch.hsy.common.core.controller.BaseController;
import com.sch.hsy.common.core.domain.AjaxResult;
import com.sch.hsy.common.core.domain.entity.SysMenu;
import com.sch.hsy.common.core.domain.entity.SysUser;
import com.sch.hsy.common.core.text.Convert;
import com.sch.hsy.common.json.JSONObject;
import com.sch.hsy.common.utils.*;
import com.sch.hsy.framework.shiro.service.SysPasswordService;
import com.sch.hsy.system.service.*;
import org.apache.commons.lang3.StringEscapeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * 首页 业务处理
 * 
 * @author ruoyi
 */
@Controller
public class SysIndexController extends BaseController
{
    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private SysPasswordService passwordService;

    @Autowired
    private ISysDeptService deptService;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private ISysBookService bookService;

    @Autowired
    private ISysReadService readService;

    // 系统首页
    @GetMapping("/index")
    public String index(ModelMap mmap)
    {
        // 取身份信息
        SysUser user = ShiroUtils.getSysUser();
        // 根据用户id取出菜单
        List<SysMenu> menus = menuService.selectMenusByUser(user);
        mmap.put("menus", menus);
        mmap.put("user", user);


        String webIndex =  "index" ;//"index-topnav"
        return webIndex;
    }

    // 锁定屏幕
    @GetMapping("/lockscreen")
    public String lockscreen(ModelMap mmap)
    {
        mmap.put("user", ShiroUtils.getSysUser());
        ServletUtils.getSession().setAttribute(ShiroConstants.LOCK_SCREEN, true);
        return "lock";
    }

    // 解锁屏幕
    @PostMapping("/unlockscreen")
    @ResponseBody
    public AjaxResult unlockscreen(String password)
    {
        SysUser user = ShiroUtils.getSysUser();
        if (StringUtils.isNull(user))
        {
            return AjaxResult.error("服务器超时，请重新登陆");
        }
        if (passwordService.matches(user, password))
        {
            ServletUtils.getSession().removeAttribute(ShiroConstants.LOCK_SCREEN);
            return AjaxResult.success();
        }
        return AjaxResult.error("密码不正确，请重新输入。");
    }

    // 切换主题
    @GetMapping("/system/switchSkin")
    public String switchSkin()
    {
        return "skin";
    }

    // 切换菜单
    @GetMapping("/system/menuStyle/{style}")
    public void menuStyle(@PathVariable String style, HttpServletResponse response)
    {
        CookieUtils.setCookie(response, "nav-style", style);
    }

    // 系统首页
    @GetMapping("/system/main")
    public String main(ModelMap mmap)
    {
        mmap.put("clazzCount", deptService.selectCountDept());
        mmap.put("studentCount", userService.selectStudentCount());
        mmap.put("bookCount", bookService.selectBookCount());
        mmap.put("recordList", JSONUtils.toJSONString(readService.selectReadRankingListIndex()));
        mmap.put("bookClassify", JSONUtils.toJSONString(bookService.selectBookClassify()));
        return "main";
    }

}
